<template>
	<view>
		<view class="content">
			<u-upload class="upload" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:maxCount="1" accept="video">
			</u-upload>
		</view>
		<view class="submit" @click="subMitViduo()">
			提交
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				imgUrl: this.$imgUrl,
				videoUrl: '',
				yg_id: '',
				mendian_id: '',
			}
		},
		onLoad() {
			var that = this;
			uni.getStorage({
				key: 'user_key',
				complete: function(res) {
					// console.log(666,res)
					that.yg_id = res.data.id;
					that.mendian_id = res.data.mendian;
				}
			})
		},
		methods: {
			//添加卫生视频审核
			subMitViduo() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/AddWsshipin',
					method: 'POST',
					data: {
						tjren: _that.yg_id,
						mdid: _that.mendian_id,
						video: _that.imgUrl + _that.videoUrl
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded" // post
					},
					success: function(res) {
						// console.log(555, res)
						if (res.statusCode == 200 && res.data.code == 200) {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title:'提交成功'
							});
						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: res.data.msg
							});
						}
					}
				})
			},
			// 删除图片
			deletePic(event) {
				// console.log(333,event)
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				// console.log(444,lists)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					// console.log(555,result)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				// console.log(777,url)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: getApp().globalData.url + '/api/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'files',
						// formData: {
						// 	user: 'test'
						// },
						success: (res) => {
							// console.log(666,res)
							if (res.statusCode == 200) {
								var data = JSON.parse(res.data)
								uni.showToast({
									icon: 'none',
									position: 'center',
									title: data.msg
								});
								this.videoUrl = data.data
							} else {
								uni.showToast({
									icon: 'none',
									position: 'center',
									title: '上传失败'
								});
							}
							setTimeout(() => {
								resolve(res)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.submit {
		height: 80rpx;
		background-color: #1BB68E;
		text-align: center;
		color: #fff;
		font-weight: bold;
		line-height: 80rpx;
		border-radius: 50rpx;
		margin: auto 70rpx;
		position: fixed;
		right: 0;
		left: 0;
		bottom: 100rpx;
	}
</style>
